<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\MenuRequest;
use App\Models\Menu;
use Illuminate\Http\Request;

class NewMenuController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // $query = Menu::query();

        // $data = $query->get()->toTree()->toArray();

        $id = $request->input('id');
// Menu::fixTree();
        if(!$id) {
            $nodeData = Menu::get();
        }else{
            $node = Menu::find($id);
            if ($node->pid == 0) {
                $nodeData = $node->siblings()->get();
            } else {
                $pnode = Menu::find($node->pid);
                $nodeData = Menu::whereNotDescendantOf($pnode)->get();
            }
        }

        $data = $nodeData->toTree()->toArray();

        $this->formatChildren($data);

        return ['data' => $data];
    }

    //格式化子数据
    protected function formatChildren(&$data)
    {
        foreach ($data as &$v) {
            if (!empty($v['children'])) {
                $this->formatChildren($v['children']);
            } else {
                unset($v['children']);
            }
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(MenuRequest $request)
    {
        // $validated = $request->validated();

        $data = $request->except('action');

        $parentId = array_get($data, 'pid', 0);

        $parent = Menu::find($parentId);

        // if ($parent) {
        //     $menu = Menu::create($data, $parent);
        // } else {
        //     $menu = Menu::create($data);
        // }
        $menu = Menu::create($data, $parent);

        if (!$menu) {
            return $this->error('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(MenuRequest $request, $id)
    {
        $data = $request->except('action');
        $menu = Menu::findOrFail($id);
        $pid = (int)array_get($data, 'pid');
        if($pid==0) {
            array_set($data, 'pid', null);
        }
        $menu->fill($data);

        if (!$menu->save()) {
            return $this->error('保存失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $menu = Menu::findOrFail($id);
        if (!$menu->delete()) {
            return $this->error('删除失败');
        }
    }
}
